﻿using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.AI;

// 中介者模式 中转 负责了其他组件对象之间的交互，降低耦合度
public abstract class ICharacter
{
    // 属性
    public bool isKilled { get { return mIsKilled; } }
    protected ICharacterAttr mAttr;
    public ICharacterAttr attr { set { mAttr = value; } get { return mAttr; } }

    // 其他组件
    protected AudioSource mAudio;
    protected NavMeshAgent mNavAgent;
    protected Animation mAnim;


    // 释放角色资源的等待时间,用于死亡动画播放完毕后销毁角色
    protected float mDestroyTimer = 2f;
    protected bool mCanDestroy = false;
    protected bool mIsKilled = false;
    public bool canDestroy { get { return mCanDestroy; } }

    // 武器
    protected IWeapon mWeapon;

    public IWeapon weapon
    {
        set
        {
            mWeapon = value;
            mWeapon.owner = this;
            GameObject child = UnityTool.FindChild(mGameObject, "weapon-point");
            UnityTool.Attach(child, mWeapon.gameObject);
        }
        get { return mWeapon; }
    }

    // 武器的攻击范围
    public float atkRange { get { return mWeapon.atkRange; } }

    // 游戏对象
    protected GameObject mGameObject;

    public GameObject gameObject
    {
        set
        {
            mGameObject = value;
            mNavAgent = mGameObject.GetComponent<NavMeshAgent>();
            mAudio = mGameObject.GetComponent<AudioSource>();
            mAnim = mGameObject.GetComponentInChildren<Animation>();
        }
        get { return mGameObject; }
    }

    // 角色的位置
    public Vector3 position
    {
        get
        {
            if (mGameObject == null)
            {
                Debug.LogError("mGameObject为空");
                return Vector3.zero;
            }

            return mGameObject.transform.position;
        }
    }

    public void Attack(ICharacter target)
    {
        // todo 攻击
        mWeapon.Fire(target.position);
        // 朝向
        mGameObject.transform.LookAt(target.position);
        PlayAnim("attack");
        // IcharacterAttr IWeapon atk ?
        target.UnderAttack(mWeapon.atk + mAttr.critValue);
    }

    public virtual void UnderAttack(int damage)
    {
        // 受伤
        mAttr.TakeDamage(damage);
        // 被攻击的效果 视效 只有敌人有
        // 死亡效果 音效 视频效果 只有战士有
    }

    protected void DoPlayEffect(string effectName)
    {
        // 资源性能消耗 todo
        GameObject effectGO = FactoryManager.assetFactory.LoadEffect(effectName);
        effectGO.transform.position = position;
        effectGO.AddComponent<DestoryForTime>();
    }

    protected void DoPlaySound(string soundName)
    {
        AudioClip clip = FactoryManager.assetFactory.LoadAudioClip(soundName);
        mAudio.clip = clip;
        mAudio.Play();
    }

    public void PlayAnim(string animName) { mAnim.CrossFade(animName); }

    public void MoveTo(Vector3 targetPosition)
    {
        // Debug.Log("targetPosition");
        // Debug.Log(targetPosition);
        mNavAgent.SetDestination(targetPosition);
        PlayAnim("move");
    }

    public void Update()
    {
        if (mIsKilled)
        {
            mDestroyTimer -= Time.deltaTime;
            if (mDestroyTimer <= 0)
            {
                mCanDestroy = true;
            }

            return;
        }

        mWeapon.Update();
    }

    public virtual void Killed()
    {
        mIsKilled = true;
        mNavAgent.Stop();
        // 关卡系统和成就系统需要知道角色的击杀生死
    }

    public abstract void UpdateFSMAI(List<ICharacter> targets);

    public void Release() { GameObject.Destroy(mGameObject); }

    public abstract void RunVisitor(ICharacterVisitor visitor);
}